package com.ych.modules.cms.authorInfc.role.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.cms.authorInfc.role.service.RoleService;
import com.ych.modules.base.dbEntity.TSysRoleEntity;
import com.ych.modules.base.service.IRSysRoleMenuService;
import com.ych.modules.base.service.ITSysRoleService;
import com.ych.utils.TLMap;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/15
 * Time: 14:27
 * Description:
 */
@Service("RoleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private ITSysRoleService     itSysRoleService;
    @Autowired
    private IRSysRoleMenuService irSysRoleMenuService;

    @Override
    public Page<TSysRoleEntity> list(TSysRoleEntity roleEntity) {
        //如果不是超级管理员，则只查询自己创建的角色列表
        if (!"1".equals(TLMap.getUserId())) {
            roleEntity.setCreateUserId(TLMap.getUserId());
        }
        Wrapper<TSysRoleEntity> wrapper = new EntityWrapper<TSysRoleEntity>()
                .where("1=1")
                .eq(StringUtils.isNotBlank(roleEntity.getCreateUserId()), "create_user_id", roleEntity.getCreateUserId())
                .like(StringUtils.isNotBlank(roleEntity.getRoleName()), "role_name", roleEntity.getRoleName());
        return itSysRoleService.selectPage(new Query<TSysRoleEntity>(BeanMapUtil.toPageQuery(roleEntity)).getPage(), wrapper);
    }

    @Override
    public List<TSysRoleEntity> select() {
        Map<String, Object> map = new HashMap<>();
        //如果不是超级管理员，则只查询自己所拥有的角色列表
        if (!"1".equals(TLMap.getUserId())) {
            map.put("create_user_id", TLMap.getUserId());
        }
        return itSysRoleService.selectByMap(map);
    }

    @Override
    public TSysRoleEntity info(String roleId) {
        TSysRoleEntity role = itSysRoleService.selectById(roleId);
        //查询角色对应的菜单
        List<String> menuIdList = irSysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);
        return role;
    }

    @Override
    public void save(TSysRoleEntity entity) {
        entity.setCreateUserId(TLMap.getUserId());
        itSysRoleService.save(entity);
    }

    @Override
    public void update(TSysRoleEntity entity) {
        entity.setCreateUserId(TLMap.getUserId());
        itSysRoleService.update(entity);
    }

    @Override
    public void delete(String[] roleIdsStr) {
        itSysRoleService.delete(roleIdsStr);
    }
}
